Ballon.Util = Ballon.Class.create();
Ballon.Util.prototype = {
    forward: function (url, callback) {
        $.ajax({
            type: "GET",
            url: url,
            success: function (content) {
                $(".content-wrapper").html(content);
                if (callback && typeof(callback) == 'function') {
                    callback();
                }
            }
        });
    },
    menu: function () {
        //构建功能菜单
        if (TIGER_MENUS) {
            for (var x in TIGER_MENUS) {
                var menu = TIGER_MENUS[x];
                var obj;
                var util = new Ballon.Util();
                if (menu.menus) {
                    obj = document.createElement("li");
                    obj.className = "treeview";
                    obj.id = menu.id;

                    var tmpNode = document.createElement("a");
                    tmpNode.href = "#";

                    var tmpHtml = "";
                    if (menu.icon) {
                        tmpHtml = "<i class='" + menu.icon + "'></i>";
                    }
                    tmpNode.innerHTML = tmpHtml + "<span>" + menu.name + "</span><span class='pull-right-container'><i class='fa fa-angle-left pull-right'></i></span>";
                    obj.appendChild(tmpNode);

                    var ulNode = document.createElement("ul");
                    ulNode.className = "treeview-menu";
                    obj.appendChild(ulNode);

                    for (var y in menu.menus) {
                        var childMenu = menu.menus[y];
                        ulNode.appendChild(util.createMenuNode(childMenu, menu.id));
                    }
                } else {
                    obj = util.createMenuNode(menu);
                }
                $(".sidebar-menu").append(obj);
            }
        }
    },
    menu_ajax: (data_url) => {
        $.ajax({
            url: data_url,
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if (data) {
                    for (var x in data.children) {
                        var menu = data.children[x];
                        var obj;
                        var util = new Ballon.Util();
                        if (menu.children) {
                            obj = document.createElement("li");
                            obj.className = "treeview";
                            obj.id = menu.id;

                            var tmpNode = document.createElement("a");
                            tmpNode.href = "#";

                            var tmpHtml = "";
                            if (menu.ico) {
                                tmpHtml = "<i class='" + menu.ico + "'></i>";
                            }
                            tmpNode.innerHTML = tmpHtml + "<span>" + menu.name + "</span><span class='pull-right-container'><i class='fa fa-angle-left pull-right'></i></span>";
                            if (menu.tag) {
                                tmpNode.innerHTML += menu.tag;
                            }
                            obj.appendChild(tmpNode);

                            var ulNode = document.createElement("ul");
                            ulNode.className = "treeview-menu";
                            obj.appendChild(ulNode);

                            for (var y in menu.children) {
                                var childMenu = menu.children[y];
                                ulNode.appendChild(util.createMenuNode(childMenu, menu.id));
                            }
                        } else {
                            obj = util.createMenuNode(menu);
                        }
                        $(".sidebar-menu").append(obj);
                    }
                }
            },
            error: (err) => {
            }
        });
    },
    init_authen: () => {
        var uri = window.location.search;
        if (uri != "") {
            const ukey = uri.substr(1);
            //登录成功后, 在每一个ajax请求中自动携带用户ukey
            $.ajaxSetup({
                beforeSend: (jqXHR, settings) => {
                    if (!settings.url.endsWith('.html') && !settings.url.endsWith('.js')) {
                        let uris = settings.url.split('?');
                        settings.url = uris[0] + '/' + ukey;
                        if (uris[1] != undefined) {
                            settings.url += '?' + uris[1];
                        }
                    }
                }
            });
        } else {
            swal("警告！", "请先登录！", "error");
        }
    },
    createMenuNode: function (menuData, parentId) {
        var node = document.createElement("li");
        node.className = "active";
        node.id = menuData.id;
        if (parentId) {
            node.setAttribute("father", parentId);
        }
        var tmpHtml = "<a href=javascript:util.forward('" + menuData.url + "')>";
        if (menuData.ico) {
            tmpHtml += "<i class='" + menuData.ico + "'></i>";
        }
        tmpHtml += menuData.name;
        if (menuData.tag) {
            tmpHtml += menuData.tag;
        }
        tmpHtml += "</a>";
        node.innerHTML = tmpHtml;
        return node;
    }
}
